package cn.dnui_wwt524.dnuifood_16110100524.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.bean.FoodBean;
import cn.dnui_wwt524.dnuifood_16110100524.controller.FoodInfoActivity;
import cn.dnui_wwt524.dnuifood_16110100524.controller.FoodListActivity;

public class FoodListAdapter extends RecyclerViewAdapter<FoodListAdapter.ViewHolder> {

    public FoodListAdapter(Context context, List data, int layoutResource) {
        super(context, data, layoutResource);
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = layoutInflater.from(parent.getContext()).inflate(R.layout.food_item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull FoodListAdapter.ViewHolder viewHolder,final int position) {
        final FoodBean entity = (FoodBean) data.get(position);
        viewHolder.food_name.setText(entity.getFoodname());
        viewHolder.food_price.setText(entity.getPrice()+"元");
        viewHolder.food_intro.setText(entity.getIntro());

        Picasso.with(context).load(FoodListActivity.Constants.BASE_URL+entity.getPic()).into(viewHolder.food_image);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = String.valueOf(entity.getFood_id());
                Intent intent = new Intent(context,FoodInfoActivity.class);
                intent.putExtra("food_id",str);
                context.startActivity(intent);

            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView food_name,food_price,food_intro;
        ImageView food_image;
        public ViewHolder(View view) {
            super(view);
            food_name=(TextView)view.findViewById(R.id.food_foodname);
            food_price=(TextView)view.findViewById(R.id.food_price);
            food_image=(ImageView)view.findViewById(R.id.food_pic);
            food_intro=(TextView)view.findViewById(R.id.food_intro);
        }
    }
    @Override
    public int getItemCount() {
        return data.size();
    }

}
